package com.jet.chat.server;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;

import com.jet.chat.command.ChatCommand;
import com.jet.chat.exception.ChatExceptionDBService;
import com.jet.chat.exception.ChatExceptionMessageRead;
import com.jet.chat.exception.ChatExceptionSessionReaderNotAvailable;
import com.jet.chat.exception.ChatExceptionSessionWriterNotAvailable;
import com.jet.chat.service.DBService;


public class ChatServer {
	private int port;
	private String dbname;
	private DBService dbService;
	private HashMap<String, ChatChannel> channels = new HashMap<String, ChatChannel>();
	private HashMap<String, ChatCommand> chatCommands = new HashMap<String, ChatCommand>();
	private HashMap<String, String> chatCommandAliases = new HashMap<String, String>();
	
	public void addChatCommandAlias(String alias, String command) {
		chatCommandAliases.put(alias, command);
	}
	
	public HashMap<String, String> getChatCommandAliases() {
		return chatCommandAliases;
	}
	
	public HashMap<String, ChatCommand> getChatCommands() {
		return chatCommands;
	}
	
	public void addChatCommand(String commandName, ChatCommand chatCommand) {
		chatCommands.put(commandName, chatCommand);
	}

	public void addChannel(ChatChannel chatChannel) {
		channels.put(chatChannel.getChannelName(), chatChannel);
	}
	
	public ChatChannel getChannel(String channelName) {
		return channels.get(channelName);
	}
	
	public HashMap<String, ChatChannel> getChannels() {
		return channels;
	}
	
	public ChatServer(int port, String dbname) {
		this.port = port;
		this.dbname = dbname;
		this.dbService = new DBService(dbname);
	}
	
	private void execute() {
		File cfgFile = new File("commands.txt");

		LineNumberReader cfgReader;
		try {
			cfgReader = new LineNumberReader(new FileReader(cfgFile.getAbsolutePath()));
			String line;
			String alias;
			String command;
			try {
				while (((line = cfgReader.readLine()) != null)) {
					alias = line.split("\\s")[0];
					command = line.split("\\s")[1];
					addChatCommandAlias(alias, command);
					//cfgReader.close();
				}

				try {
					ServerSocket serverSocket = new ServerSocket(port);
					System.out.println("server started");
					dbService.createConnection();
					
					ChatChannel chatChannel = new ChatChannel("Default");
					addChannel(chatChannel);
					
					Socket socket = null;
					Thread session = null;
					while (true) {
						socket = serverSocket.accept();
						session = new Thread(new ChatSession(this, "Default", socket));
						session.start();
					}
					
				} catch (IOException e) {
					System.out.println("здрасьте, порт уже занят");
					//e.printStackTrace();
				} catch (ChatExceptionSessionReaderNotAvailable e) {
					e.printStackTrace();
				} catch (ChatExceptionSessionWriterNotAvailable e) {
					e.printStackTrace();
				} catch (ChatExceptionMessageRead e) {
					e.printStackTrace();
				} catch (ChatExceptionDBService e) {
					e.printStackTrace();
				} finally {
					dbService.shutdown();
				}
			} catch (IOException e1) {
				System.out.println("config file can not be read. chat commands will disabled");
				//e1.printStackTrace();
			}
		} catch (FileNotFoundException e1) {
			System.out.println("config file not found. chat commands will disabled");
			//e1.printStackTrace();
		}

	}
	
	public static void main(String[] arg) {
		ChatServer chatServer = new ChatServer(30030, "chatdb");
		chatServer.execute();
	}

	public String getDbname() {
		return dbname;
	}

	public DBService getDbService() {
		return dbService;
	}

}
